package com.runfast.waimai.web.controller.data;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.paotui.dao.model.RunfastCuser;
import com.runfast.paotui.dao.model.RunfastCuserExample;
import com.runfast.paotui.service.RunfastCuserService;
import com.runfast.waimai.dao.mapper.RunfastDataCenterMapper;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentaccounts;
import com.runfast.waimai.service.RunfastBusinessDailyService;
import com.runfast.waimai.utils.MyUtil;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by WQ on 2018/6/5.
 */
@RestController
@RequestMapping("/server/data")
public class OrderDataController {
    @Autowired
    private RunfastBusinessDailyService businessDailyService;
    @Autowired
    private RunfastDataCenterMapper dataCenterMapper;
    @Autowired
    private RunfastCuserService cuserService;

    @GetMapping("/order")
    public Result order(Integer id, String startDate, String endDate) {
        Validate.notEmpty(startDate, "开始时间不可为空");
        Validate.notEmpty(endDate, "结束时间不可为空");
        Map<String, Object> map = new HashMap<>();
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts = SessionContext.getCurrentRunfastAgentaccounts();
        if (admin == null && agentAccounts == null) {
            return Result.fail(ResultCode.ACCOUNT_RELOGIN);
        }
        if (agentAccounts != null) {
            id = agentAccounts.getAgentid();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date start = null;
        try {
            start = sdf.parse(startDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.fail(ResultCode.FAIL, "开始日期格式错误");
        }
        Date end = null;
        try {
            end = sdf.parse(endDate);
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.fail(ResultCode.FAIL, "结束日期格式错误");
        }
        List<String> dateList = new ArrayList<>();
        Long time = start.getTime();
        Long endTime = end.getTime();
        Long day = 24 * 3600 * 1000L;
        while (time < endTime) {
            Date d = new Date(time);
            dateList.add(sdf.format(d));
            time += day;
        }
        end = MyUtil.getEndDay(end);
        //有序哈希
        Map<String, Map> mr = new LinkedHashMap<>();
        dateList.forEach(x -> {
            Map<String, Object> m1 = new HashMap<>();
            m1.put("date", x);
            mr.put(x, m1);
        });
        List<Map<String, Object>> registerDaily = dataCenterMapper.registerDaily(start, end);
        registerDaily.forEach(x -> mr.put(x.get("date").toString(), x));
        List<Map> r = new ArrayList<>();
        mr.forEach((x, y) -> r.add(y));
        map.put("registerDaily", r);
        Integer[] types = new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
        //有序哈希
        Map<String, Map> mo = new LinkedHashMap<>();
        dateList.forEach(x -> {
            Map<String, Object> m1 = new HashMap<>();
            m1.put("date", x);
            mo.put(x, m1);
        });
        List<Map<String, Object>> orderDaily = dataCenterMapper.orderDaily(id, start, end, Arrays.asList(types));
        orderDaily.forEach(x -> mo.put(x.get("date").toString(), x));
        List<Map> o = new ArrayList<>();
        mo.forEach((x, y) -> o.add(y));
        map.put("registerDaily", r);
        map.put("orderDaily", o);
        return Result.ok("", map);
    }
}
